# -*- coding: utf-8 -*-
__author__ = 'sunnychou'
__date__ = '2019/7/19 14:19'

'''
外购入库单
'''

from common.orm_helper import *
from common.excel_helper import ExcelReader
from k3_data_report.import_tb.utils import fetch_tb_unique_his_data
from  common.logger_helper import  g_wlogger

##########################################
# 4 导入k3基础表  外购入库单  BEGIN
##########################################
class  WaigouList(Base):
    __tablename__ = "k3_waigourk"  # 自定义数据库表名称
    id = Column(Integer, primary_key=True)  # 自增
    sjsj = Column(String(10), verbose_name='年月')
    pingpai = Column(String(20), verbose_name='品牌')
    guige_xinghao = Column(String(80), verbose_name='规格型号')
    yinfuk_date = Column(String(20), verbose_name='应付款日期')
    shenhe_flag = Column(String(10), verbose_name='审核标志')
    ruku_code = Column(String(50), verbose_name='入库单编号')
    gongys = Column(String(100), verbose_name='供应商')
    shouliao_ck = Column(String(20), verbose_name='收料仓库')
    wuliao_code = Column(String(20), verbose_name='物料代码')
    wuliao_long_code =  Column(String(100), verbose_name='物料长代码')
    wuliao_name = Column(String(100), verbose_name='物料名称')
    unit  = Column(String(100), verbose_name='单位')
    pihao = Column(String(100), verbose_name='批号')
    weishui_uprice = Column(String(50), verbose_name='未税单价')
    yinshou_number = Column(String(50), verbose_name='应收数量')
    shishou_number = Column(String(50), verbose_name='实收数量')
    weishui_jine = Column(String(100), verbose_name='未税金额')
    shenhe_name = Column(String(100), verbose_name='审核人')
    gouji_number = Column(String(20), verbose_name='已钩稽数量')
    gouji_jine =  Column(String(20), verbose_name='已钩稽金额')
    weigouji_number = Column(String(20), verbose_name='未钩稽数量')
    weigouji_jine = Column(String(20), verbose_name='未钩稽金额')
    yuandan_code = Column(String(20), verbose_name='源单单号')
    yuandan_type = Column(String(20), verbose_name='源单类型')
    shenhe_date = Column(String(20), verbose_name='审核日期')
    dpartment = Column(String(20), verbose_name='部门')
    yewu_name = Column(String(20), verbose_name='业务员')
    kaipiao_number = Column(String(20), verbose_name='开票数量')
    changyong_unit = Column(String(20), verbose_name='常用单位')
    changyong_ysnumber = Column(String(20), verbose_name='常用单位应收数量')
    changyong_ssnumber = Column(String(20), verbose_name='常用单位实收数量')
    pc_date = Column(String(20), verbose_name='生产/采购日期')
    caigou_fangshi = Column(String(20), verbose_name='采购方式')
    hanshui_uprice = Column(String(20), verbose_name='含税单价')
    hanshui_jine = Column(String(20), verbose_name='含税金额')
    hanshui_rate = Column(String(20), verbose_name='税率')
    ruku_date = Column(String(20), verbose_name='入库日期')


def  waigoud_process(excel_name, sheet_name):
    '''
    外购入库处理过程
    :param excel_name:
    :param sheet_name:
    :return:
    '''
    ereader = ExcelReader(excel_name, sheet_name=sheet_name)
    headers = ereader.get_excel_headers()
    data_lists = ereader.get_excel_data_list()
    danju_date = data_lists[0][2]
    danju_month = "%s-%s" % (danju_date.split("-")[0], danju_date.split("-")[1])
    unique_select_sql = "select  distinct sjsj  from k3_waigourk"
    unique_set = fetch_tb_unique_his_data(unique_select_sql)
    if danju_month in unique_set:
        #print(f"waigoud_process month:{danju_month} already existed.")
        already_info = f"waigoud_process month:{danju_month} already existed."
        g_wlogger.winfo(already_info)
        return

    for data_list in data_lists:
        guige_xinghao = data_list[1]
        if guige_xinghao == "":
            continue
        gyslist = WaigouList(sjsj=danju_month, pingpai= data_list[0], guige_xinghao=guige_xinghao, yinfuk_date=data_list[2],
                                   shenhe_flag=data_list[3], ruku_code=data_list[4],gongys=data_list[5], shouliao_ck=data_list[6],
                                   wuliao_code=data_list[7], wuliao_long_code=data_list[8],wuliao_name=data_list[9],unit=data_list[10],
                                    pihao = data_list[11],weishui_uprice=data_list[12],yinshou_number=data_list[13],shishou_number=data_list[14],
                                  weishui_jine = data_list[16],shenhe_name=data_list[17],gouji_number=data_list[18], gouji_jine=data_list[19],
                                  weigouji_number = data_list[20], weigouji_jine=data_list[21],yuandan_code=data_list[25],
                                  yuandan_type = data_list[26],shenhe_date=data_list[27],dpartment=data_list[30],yewu_name=data_list[31],
                                   kaipiao_number = data_list[32],changyong_unit=data_list[36],changyong_ysnumber=data_list[37],
                                  changyong_ssnumber = data_list[38],pc_date=data_list[39],caigou_fangshi=data_list[40],
                                   hanshui_uprice = data_list[42],hanshui_jine=data_list[43],hanshui_rate=data_list[44],
                                   ruku_date=data_list[45])
        session.add(gyslist)
        session.commit()

    skd_succ = f"外购入库单 文件：{excel_name}， 日期：{danju_month}处理完毕！"
    g_wlogger.winfo(skd_succ)



def waigoud_process_main(excel_name, sheet_name):
    try:
        waigoud_process(excel_name, sheet_name)
        return True
    except Exception as e:
        g_wlogger.werror(f"waigoud_process_main has error:{e} with excel:{excel_name}")
        return False

##########################################
# 4 导入k3基础表  外购入库单  END
##########################################


